"""
    爬虫
"""
import json
import time

import requests
import base64
from requests.utils import resolve_proxies

# 要解码的 Base64 字符串
#encoded_string = 'eyJ0ZW1wbGF0ZV9pZCI6Ijk5MTM0NTMwMjIyMjkyOTkyMCIsImZpbHRlcl9jb25kaXRpb25zIjoiIiwiZHluYW1pY19wYXJhbXMiOnt9fQ=='
# 将字符串编码为字节类型
#encoded_bytes = encoded_string.encode('utf-8')
# 进行 Base64 解码
#decoded_bytes = base64.b64decode(encoded_bytes)
# 将解码后的字节转换为字符串
#decoded_string = decoded_bytes.decode('utf-8')
#print(decoded_string)

page = {
    "pageNo": 1,
    "limit": 20
}
while True:
    req = requests.post(
        url="https://uat-fssc.luhua.cn/ftmp/v3/data/template/queryDataTable?t=1740367018050&locale=zh_CN",
        headers={
            "__sTokenId": "AAECAzY4Rjc2NUMwNjhGN0Q1RjkwMDAwMjI1MWNJJTIxQSP3b82SMerN4%2Bcf1xU6",
            "Content-Type": "application/json;charset=UTF-8"
        },
        json={
            "parameters": [
                {
                    "key": "response_data",
                    "value": "eyJ0ZW1wbGF0ZV9pZCI6Ijk5MTM0NTMwMjIyMjkyOTkyMCIsImZpbHRlcl9jb25kaXRpb25zIjoiIiwiZHluYW1pY19wYXJhbXMiOnt9fQ=="
                },
                {
                    "key": "filter_condition_key"
                }
            ],
            "requestPage": page,
            "sorts": [],
            "isEncode": "Y",
            "encodeParam": [
                "response_data"
            ]
        }
    )
    dataResult = req.json().get('data', {}).get('dataResult', [])  # 避免键不存在时报错
    if isinstance(dataResult, list) and len(dataResult) > 0:
        time.sleep(1)
        with open("../logs/boe_no.log", "a+", encoding="UTF-8") as f:
            for item in dataResult:
                print(f"单据编号：{item["单据编号"]}，填单人：{item["报账人ID"]}， 报账金额：{item["报账金额"] : < 12}，单据摘要：{item["单据摘要"]}")
                f.writelines(f"\n单据编号：{item["单据编号"]}，填单人：{item["报账人ID"]}， 报账金额：{item["报账金额"] : < 12}，单据摘要：{item["单据摘要"]}")
    else:
        break



